package br.ufrn.opencopi.init;

import br.ufrn.opencopi.exceptions.PortException;
import br.ufrn.opencopi.exceptions.UnknownMiddlewareTypeException;

public class OpencopiInit {

	static Opencopi opencopi;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			if(args[0] != null && isValidPort(args[1]))
				opencopi = new Opencopi(args[0], Integer.parseInt(args[1]));
			else
				opencopi = new Opencopi(); //using default host and port. See Opencopi.DEFAULTPORT

			opencopi.init();
			System.out.println("Opencopi Pronto");

		} catch (Exception e) {
			if(e instanceof PortException)
				System.out.println("Port Exception: This port number couldn't be used. \n"+e.getMessage());

			else{
				System.out.println("OpenCOPI Exception: "+e.getMessage()); //RMI exceptions
				e.printStackTrace();
			}
		}



	}

	/**
	 * Verify if the Opencopi service name is valid.
	 * @param serviceName
	 * @return boolean
	 */
	private static boolean isValidServiceName(String serviceName) {
		if(serviceName.length() > 0)
			return true;
		return false;
	}

	/**
	 * Verify if the port wanted is or no valid.
	 * For us, is valid a por between 50.000 and 59.999.
	 * @param string
	 * @return boolean
	 * @throws PortException
	 */
	private static boolean isValidPort(String string) throws PortException {
		int port = Integer.parseInt(string);
		if(port <50000 || port >= 60000){
			throw new PortException("Invalid port. Please choose a port between 50.000 and 59.999!");
		}
		return true;
	}

}
